package org.jeecg.modules.smoke.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.smoke.entity.LwgyJointSmokeConfiguration;
import org.jeecg.modules.smoke.mapper.LwgyJointSmokeConfigurationMapper;
import org.jeecg.modules.smoke.service.ILwgyJointSmokeConfigurationService;

import org.jeecg.modules.smoke.util.SocketClientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Description: 设备管理
 * @Author: jeecg-boot
 * @Date:   2024-01-25
 * @Version: V1.0
 */
@Service
public class LwgyJointSmokeConfigurationServiceImpl extends ServiceImpl<LwgyJointSmokeConfigurationMapper, LwgyJointSmokeConfiguration> implements ILwgyJointSmokeConfigurationService {

    @Resource
    private LwgyJointSmokeConfigurationMapper lwgyJointSmokeConfigurationMapper;


    private  final String IP="192.168.0.120";
    private  final int PORT=7000;

    private  final String HEAD="8778";
    private  final String FIX="03";
    private  final String AQORDER="2B";//安全指令
    private  final String TIMEORDER="36";//时间指令
    private  final String ALAEMORDER="2C";//警报指令

    private  final String TYPE="10";//上传查询
    private  final String TYPE2="00";//设置

  /*  @Resource
    private ILwgyJointSmokeDailyService iLwgyJointSmokeDailyService;*/

    @Override
    public IPage<LwgyJointSmokeConfiguration> querySmokeDeviceList(Page<LwgyJointSmokeConfiguration> page, QueryWrapper<LwgyJointSmokeConfiguration> queryWrapper) {
        queryWrapper.orderByAsc("building_name","unit_name","layer_name","room_name");
        return page.setRecords(lwgyJointSmokeConfigurationMapper.querySmokeDeviceList(page,queryWrapper));
    }

    public Boolean queryAQByDeviceNum(LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration){
        try {
            String deviceNumber = lwgyJointSmokeConfiguration.getDeviceNumber();
            String queryTimeHex = HEAD + deviceNumber + FIX + AQORDER + TYPE + "00";//拼接查询时间的数据
            String addsum = addHexStrings(queryTimeHex);
            String endString = queryTimeHex + addsum;
            // byte[] queryCommand2 = hexStringToByteArray(queryTimeHex+addsum);
            System.out.println("发送安全查询指令 " + endString);
            SocketClientService socketClientService = new SocketClientService();
            socketClientService.sendDataToServer(IP, PORT, endString);

            return true;
        }catch (Exception e){
            return false;
        }
    }


    public Boolean queryTimeByDeviceNum(LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration){
        try {
            String deviceNumber = lwgyJointSmokeConfiguration.getDeviceNumber();
            String queryTimeHex = HEAD + deviceNumber + FIX + TIMEORDER + TYPE + "00";//拼接查询时间的数据
            String addsum = addHexStrings(queryTimeHex);
            String endString = queryTimeHex + addsum;
            // byte[] queryCommand2 = hexStringToByteArray(queryTimeHex+addsum);
            System.out.println("发送时间查询指令 " + endString);
            SocketClientService socketClientService = new SocketClientService();
            socketClientService.sendDataToServer(IP, PORT, endString);

            return true;
        }catch (Exception e){
            return false;
        }
    }

   public Boolean setTimeToDeviceNum(LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration){
        try {
            String deviceNumber = lwgyJointSmokeConfiguration.getDeviceNumber();
            String queryTimeHex = HEAD + deviceNumber + FIX + TIMEORDER + TYPE2 + "07";//拼接查询时间的数据

            LocalDateTime now = LocalDateTime.now();

            // 将年、月、日、时、分、秒转换为十六进制
            int year = now.getYear();
            int month = now.getMonthValue();
            int day = now.getDayOfMonth();
            int hour = now.getHour();
            int minute = now.getMinute();
            int second = now.getSecond();
            // 打印十六进制表示的时间
            String yearHex = String.format("%04X", year);  // %04X 表示输出4位大写十六进制数，不足位数用0填充
            String monthHex = String.format("%02X", month); // %02X 表示输出2位大写十六进制数，不足位数用0填充
            String dayHex = String.format("%02X", day);
            String hourHex = String.format("%02X", hour);
            String minuteHex = String.format("%02X", minute);
            String secondHex = String.format("%02X", second);

            String data = queryTimeHex+yearHex+monthHex+dayHex+hourHex+minuteHex+secondHex;//拼接数据区
            System.out.println("data:"+data);

            String addsum = addHexStrings(data);
            String endString = data + addsum;
            System.out.println("发送时间查询指令 " + endString);
            SocketClientService socketClientService = new SocketClientService();
            socketClientService.sendDataToServer(IP, PORT, endString);

            return true;
        }catch (Exception e){
            return false;
        }
    }

    public Boolean editAq(LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration){
        try {
            String deviceNumber = lwgyJointSmokeConfiguration.getDeviceNumber();
            String queryTimeHex = HEAD + deviceNumber + FIX + AQORDER + TYPE2 + "05";//拼接烟雾下限 温度下限 周期


            int smokeValue=lwgyJointSmokeConfiguration.getDeviceSmoke();
            double temperatureValue = lwgyJointSmokeConfiguration.getDeviceTemperature();
            int cycle = lwgyJointSmokeConfiguration.getCycle();

            String smokeHex = String.format("%04X", smokeValue);
            String temperatureHex = String.format("%04X",(int) Math.floor(temperatureValue*10));
            String cycleHex = String.format("%02X", cycle);

            String data = queryTimeHex+smokeHex+temperatureHex+cycleHex;//拼接数据区
            System.out.println("data:"+data);

            String addsum = addHexStrings(data);
            String endString = data + addsum;
            System.out.println("发送安全查询指令 " + endString);
            SocketClientService socketClientService = new SocketClientService();
            socketClientService.sendDataToServer(IP, PORT, endString);

            return true;
        }catch (Exception e){
            return false;
        }
    }
  public Boolean AlarmClear(String deviceNumber){
        try {
            String queryTimeHex = HEAD + deviceNumber + FIX + ALAEMORDER + TYPE2 + "01";//拼接烟雾下限 温度下限 周期
            String data = queryTimeHex+String.format("%02X", 17);//拼接数据区 D9(十六进制数） 11 禁止本次报警；22 不禁止本次报警。

            System.out.println("data:"+data);

            String addsum = addHexStrings(data);
            String endString = data + addsum;
            System.out.println("发送禁止警报指令 " + endString);//8778010102032C00011144
            SocketClientService socketClientService = new SocketClientService();
            socketClientService.sendDataToServer(IP, PORT, endString);

            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static String addHexStrings(String hexString) {
        // 每两位分组并计算总和
        int sum = 0;
        for (int i = 0; i < hexString.length(); i += 2) {
            String hexPair = hexString.substring(i, i + 2);
            int decimalValue = Integer.parseInt(hexPair, 16); // 将16进制字符串转换为10进制数
            sum += decimalValue;
        }

        // 取模256得到舍去高位的结果
        int result = sum % 256;
        // 将结果转换为2位的16进制字符串（若不足两位，前面补0）
        String hexResult = String.format("%02X", result);
        return hexResult;
    }

}
